import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../../components/toast_utils.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../login/controller/webview.dart';
import '../../manger/global_config.dart';
import '../controller/logout_pagethree.dart';
import '../../manger/qysdk_manger.dart';
class ZhuxiaoStepTwoPage extends StatefulWidget {
  final String reasonText;

  const ZhuxiaoStepTwoPage({Key? key, required this.reasonText}) : super(key: key);

  @override
  _ZhuxiaoStepTwoPageState createState() => _ZhuxiaoStepTwoPageState();
}

class _ZhuxiaoStepTwoPageState extends State<ZhuxiaoStepTwoPage> {
  bool isAgree = false;

  final TextEditingController _reasonController = TextEditingController();
  
  @override
  void initState() {
    super.initState();
    _reasonController.text = widget.reasonText;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        title: Text(AppLocalizations.of(context)!.closeAccount,style: const TextStyle(fontSize: 17)),
        actions: [
          TextButton.icon(
            onPressed: () {
              QYSDK.showQYSession();
            },
            icon: Image.asset('assets/images/zhuxiao0.png'),
            label: Text(isChineseLocale() ? '客服' : 'Customer',style: const TextStyle(fontSize: 16)),
            style: TextButton.styleFrom(
              foregroundColor: Colors.black,
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(0.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Title Text
            Padding(
              padding: const EdgeInsets.only(top: 16,left: 12,right: 12),
              child: Text(
                AppLocalizations.of(context)!.logout1,
                style: TextStyle(fontSize: 14, color: Colors.blue),
                maxLines: 2,
              ),
            ),

            // Instruction Text
            Padding(
              padding: const EdgeInsets.only(top: 10),
              child: Container(
                color: Colors.white,
                padding: const EdgeInsets.all(12),
                child: Text(
                  AppLocalizations.of(context)!.logout2,
                style: TextStyle(fontSize: 14, color: Colors.black),
              ),
            ),
            ),

            // Agree Protocol
            Padding(
              padding: const EdgeInsets.only(top: 20),
              child: Row(
                children: [
                  Checkbox(
                    value: isAgree,
                    onChanged: (value) {
                      setState(() {
                        isAgree = value!;
                      });
                    },
                  ),
                  Expanded(
                    child: GestureDetector(
                      onTap: _openProtocol,
                      child: Text(
                        AppLocalizations.of(context)!.logout3,
                        style: TextStyle(fontSize: 14, color: Colors.blue),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ),
                ],
              ),
            ),

            // Next Button
            Spacer(),
            Padding(
              padding: const EdgeInsets.only(left: 30.0, right: 30.0, bottom: 40.0),
              child: ElevatedButton(
                onPressed: _onNextPressed,
                child: Text(AppLocalizations.of(context)!.logout4, style: TextStyle(color: Colors.white)),
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue,
                  foregroundColor: Colors.white,
                  minimumSize: Size(double.infinity, 50),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // Open protocol page (show WebView for terms)
  void _openProtocol() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => WebViewPage(
          title: AppLocalizations.of(context)!.news,
          url: UrlConfig.zhuxiaoURL(context),
        ),
      ),
    );
  }

  // Next Button Click action
  void _onNextPressed() {
    if (isAgree) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => LogoutPageThree(reasonText: widget.reasonText),
        ),
      );
    } else {
      ToastUtils.showCenterToast(context, "请先阅读并同意注销协议");
    }
  }
}


